package com.hm.admin.service;

import com.hm.admin.dao.mapper.HmAdminPermissionMapper;
import com.hm.admin.domain.HmAdminPermission;
import com.hm.admin.domain.HmAdminPermissionExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class HmAdminPermissionService {
    @Resource
    private HmAdminPermissionMapper permissionMapper;

    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();
        if(roleIds.length == 0){
            return permissions;
        }

        HmAdminPermissionExample example = new HmAdminPermissionExample();
        example.or().andRoleIdIn(Arrays.asList(roleIds)).andDeletedEqualTo(false);
        List<HmAdminPermission> permissionList = permissionMapper.selectByExample(example);

        for(HmAdminPermission permission : permissionList){
            permissions.add(permission.getPermission());
        }

        return permissions;
    }


//    public Set<String> queryByRoleId(Integer roleId) {
//        Set<String> permissions = new HashSet<String>();
//        if(roleId == null){
//            return permissions;
//        }
//
//        HmAdminPermissionExample example = new HmAdminPermissionExample();
//        example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
//        List<HmAdminPermission> permissionList = permissionMapper.selectByExample(example);
//
//        for(HmAdminPermission permission : permissionList){
//            permissions.add(permission.getPermission());
//        }
//
//        return permissions;
//    }
//
//    public boolean checkSuperPermission(Integer roleId) {
//        Set<String> permissions = new HashSet<String>();
//        if(roleId == null){
//            return false;
//        }
//
//        HmAdminPermissionExample example = new HmAdminPermissionExample();
//        example.or().andRoleIdEqualTo(roleId).andPermissionEqualTo("*").andDeletedEqualTo(false);
//        return permissionMapper.countByExample(example) != 0;
//    }
//
//    public void deleteByRoleId(Integer roleId) {
//        HmAdminPermissionExample example = new HmAdminPermissionExample();
//        example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
//        permissionMapper.logicalDeleteByExample(example);
//    }
//
//    public void add(HmAdminPermission hmAdminPermission) {
//        hmAdminPermission.setAddTime(LocalDateTime.now());
//        hmAdminPermission.setUpdateTime(LocalDateTime.now());
//        permissionMapper.insertSelective(hmAdminPermission);
//    }
}
